class Game {
  controller: Gamepad;
  turbo: boolean;
  onConnect
  constructor({update, connect}: {update: Function, connect: Function}) {
    // this.init()
    this.controller = null as any;
    this.onConnect = connect;
    this.turbo = false;
    const runAnimation = () => {
        // console.log(Date.now())
      window.requestAnimationFrame(runAnimation);
      this.update(update);
    };
    window.requestAnimationFrame(runAnimation);
  }
  update(func: Function) {
    let gamepad = navigator
    .getGamepads()[0]
    let index = 
    gamepad?.buttons?.findIndex((item) => !!item.pressed);
      if(index !== -1){
    console.log(navigator
      .getGamepads()[0])

        func(index, gamepad?.timestamp);
      }
  }
  connect(evt: GamepadEvent) {
    this.controller = evt.gamepad;
    this.turbo = true;
    this.onConnect && this.onConnect(evt.timeStamp, Date.now())
    // gamepadAPI.update();
    console.log("控制器已连接。");
  }
  disconnect() {
    this.turbo = false;
  }
}

export default Game;
